package com.rdc.example.dao.impl;


import java.sql.*;

import static com.rdc.example.util.DBUtil.closeResources;
import static com.rdc.example.util.DBUtil.getConnection;

public class CommentLikeDaoImpl implements com.rdc.example.dao.CommentLike {

    public static void main(String[] args) throws Exception {
        CommentLikeDaoImpl commentLikeDao = new CommentLikeDaoImpl();
        commentLikeDao.deleteLikeByCommentId(6);
    }

    /**
     *
     * @param userId
     * @param commentId
     * @return
     * @throws Exception
     */
    @Override
    public boolean insertLike(Integer userId, Integer commentId) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = getConnection();
            String sql = "insert into `comment_like` (user_id, comment_id) values (?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, userId);
            pstmt.setInt(2, commentId);
            int row = pstmt.executeUpdate();
            return row > 0;
        } catch (SQLException e) {
            if (e.getErrorCode() == 1062) {
                System.out.println("重复点赞: user=" + userId + ", comment=" + commentId);
            } else {
                e.printStackTrace();
            }
            return false;
        } finally {
            closeResources(null, pstmt, conn);
        }
    }

    /**
     *
     * @param userId
     * @param commentId
     * @return
     * @throws Exception
     */
    @Override
    public boolean deleteLike(Integer userId, Integer commentId) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = getConnection();
            String sql = "delete from `comment_like` where user_id = ? and comment_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, userId);
            pstmt.setInt(2, commentId);
            int row = pstmt.executeUpdate();
            return row > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            closeResources(null, pstmt, conn);
        }
    }

    /**
     *
     * @param commentId
     * @return
     * @throws Exception
     */
    @Override
    public boolean deleteLikeByCommentId(Integer commentId) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = getConnection();
            String sql = "delete from `comment_like` where  comment_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, commentId);
            int row = pstmt.executeUpdate();
            return row > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            closeResources(null, pstmt, conn);
        }
    }

    /**
     *
     * @param userId
     * @param articleId
     * @return
     * @throws Exception
     */
    @Override
    public boolean likeCheck(Integer userId, Integer articleId) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            String sql = "select count(*) from `comment_like` where user_id = ? and comment_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, userId);
            pstmt.setInt(2, articleId);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                int count = rs.getInt(1);
                return count > 0;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            closeResources(rs, pstmt, conn);
        }
    }


}
